import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/inspectreport.dart';
import '../../pages/inspectreport/inspectReport_add.dart';
import '../../pages/station/station_helper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../model/stationequip.dart';
import 'package:wakelock/wakelock.dart';

class InspectAddNewPage extends StatefulWidget {
  @override
  InspectAddNewPage();

  @override
  InspectAddNewPageState createState() => new InspectAddNewPageState();
}

class InspectAddNewPageState extends State<InspectAddNewPage> {
  List<String> typeList = {"车巡", "船巡", "步巡"}.toList();
  List<String> zoneList = {"近坝区", "水库库区"}.toList();
  bool ifLoaded = false;
  late InspectReport x1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();

  Future<bool> ifCarryOn() async {
    if (Helper.localDB.containsKey("InspectData")) {
      var d1 = await Helper.localDB.getString("InspectData");

      x1 = InspectReport.fromJson(jsonDecode(d1!));
      return true;
      // var sjc = getTMFZ(x1);
      // if (sjc > 60) {
      //   return false;
      // } else {
      //   return true;
      // }
    }
    return false;
  }

  int getTMFZ(InspectReport s1) {
    var bTm = DateTime.tryParse(s1.CreateTM);
    if (bTm == null) return 0;
    var now = DateTime.now();
    var sjc = now.difference(bTm).inMinutes;
    return sjc;
  }

  void beginGo() async {
    String guid;
    String bTm;
    //  await Helper.localDB.remove("InspectGuid");
    //Wakelock.enable();
    //location = LatLng(42.0, 123.0);
    var ifBegin = await ifCarryOn();

    if (!ifBegin) {
      bTm = Helper.DateTimeNowString();
      x1 = InspectReport(
          0,
          Helper.GetGuid(),
          zoneList[0],
          UIData.CurrentUser!.Guid,
          bTm,
          UIData.CurrentUser!.Name,
          typeList[0],
          bTm,
          0,
          " ",
          " ",
          " ",
          " ",
          0.0,
          0.0,
          "正常",
          "正常",
          "无",
          "正常",
          "零星",
          "正常",
          "无",
          " ",
          " ",
          " ",
          " ",
          " ");

      c1.text = x1.InspectType;
      c2.text = x1.Zone;
      c3.text = x1.Members;

      setState(() {
        ifLoaded = true;
      });
    } else {
      Navigator.pushReplacement(context,
          new MaterialPageRoute(builder: (context) {
        return InspectReportAddPage(x1, (a) => {});
      }));
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    beginGo();
  }

  upload(BuildContext context) async {
    //x1.InspectType= c1.text;
    //x1.Zone=c2.text;
    x1.Members = c3.text;

    Helper.localDB.setString("InspectData", jsonEncode(x1));
    //print(x1.toJson());
    //RouteHelper.ShowPage(context, InspectReportAddPage(x1,null));
    Navigator.of(context)
        .pushReplacement(new MaterialPageRoute(builder: (context) {
      return InspectReportAddPage(x1, (a) => {});
    }));
    //Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(
        // title: bottomTabs[currentIndex].title,
        title: Text("库区巡查"),
      ),
      body: InspectAddNewBody(context),
    );
  }

  void onZoneChange(dynamic nn) {
    String yy = nn.toString();
    setState(() {
      x1.Zone = yy;
    });
  }

  void onInspectTypeChange(dynamic nn) {
    String yy = nn.toString();
    setState(() {
      x1.InspectType = yy;
    });
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    if (typeList == null) return list1;

    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.InspectReportImage(),
        title: Text(
          x1.CreateTM,
        ),
      ),
    ));

    list2.add(
      InputWidget.getSelect(
          "巡查区域", zoneList, x1.Zone == null ? zoneList[0] : x1.Zone,
          onChanged: onZoneChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));

    // list2.add(InputWidget.getInput("设备状态", c2));
    list2.add(InputWidget.getSelect("巡查方式", typeList, x1.InspectType,
        onChanged: onInspectTypeChange));

    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput2("巡查人员", c3));

    list2.add(Divider(
      height: 0.0,
    ));

    list1.add(InputWidget.inCard2(list2));

    list1.add(InputWidget.inCard(
        InputWidget.getButton("开始巡查", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  InspectAddNewBody(BuildContext context) => SingleChildScrollView(
        child: InspectAddNewFields(context),
      );

  InspectAddNewFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
